/*
* @description: 新增编辑页(选择商品)
* @Author: along
* @Date: 2020-11-14
* @Last Modified by: along
* @Last Modified time: 2020-11-17
*/
<template>
    <div
        class="couponChangeShop"
        v-if="dialogVisible"
    >
        <el-dialog
            title="选择商品"
            :visible.sync="dialogVisible"
            width="1200px"
            :close-on-click-modal="false"
        >
            <div class="wrap">
                <div class="headerNewBos">
                    <div class="btn-view">
                        <p
                            class="header_system"
                            @click="goShopList()"
                        >
                            商品管理
                        </p>
                        <p
                            class="header_refresh"
                            @click="refresh()"
                        >
                            刷新
                        </p>
                    </div>
                    <div class="filter-view">
                        <el-checkbox
                            v-model="formData.status"
                            @change="getTableData(1)"
                            style="margin-right: 12px"
                        >只显示已上架
                        </el-checkbox>
                        <el-select
                            size="medium"
                            v-model="formData.goodsType"
                            placeholder="商品类型"
                            clearable
                            @change="getTableData(1)"
                            style="margin-right: 12px"
                        >
                            <el-option
                                v-for="item in goodsTypeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                        <el-cascader
                            placeholder="请选择"
                            :options="classifyList"
                            filterable
                            clearable
                            change-on-select
                            expand-trigger="click"
                            v-model="formData.type_no"
                            size="medium"
                            @change="getTableData(1)"
                            :props="{
                                label: 'title',
                                value: 'no',
                                children: 'children'
                            }"
                        />
                        <div class="filter">
                            <div class="filter_keyword">
                                <el-input
                                    v-model="formData.keyword"
                                    placeholder="搜索商品编号、商品名称、店铺名称、公司名称"
                                    size="medium"
                                    clearable
                                    suffix-icon="el-icon-search"
                                    @change="getTableData(1)"
                                    class="placeStyle"
                                />
                            </div>
                        </div>
                    </div>
                </div>

                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    border
                    ref="couponChangeShop"
                    class="tableDate"
                    v-loading="loading"
                    row-key="goods_no"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column
                        type="selection"
                        :select="true"
                        :reserve-selection="true"
                        width="40"
                        v-if="dialogType === 2"
                    />
                    <el-table-column
                        prop="goods_cover"
                        label="商品信息"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="left"
                        width="200px"
                    >
                        <template slot-scope="scope">
                            <div class="info">
                                <img
                                    :src="scope.row.goods_cover"
                                    :style="{width: '64px', height: '64px', marginRight: '12px'}"
                                    @click="fnShowPreview(scope.row.goods_cover)"
                                >
                                <div class="info_main">
                                    <p class="info_title">
                                        {{ scope.row.goods_title }}
                                    </p>
                                    <p class="info_price">
                                        ¥{{ scope.row.goods_price }}
                                    </p>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_no"
                        label="商品编号"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="goods_type"
                        label="商品类型"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <p v-if="scope.row.goods_type === 1">自营</p>
                            <!-- <p v-else-if="scope.row.goods_type == 3">秒杀</p> -->
                            <p v-else-if="scope.row.goods_type === 5">折扣专区</p>
                            <!-- <p v-else-if="scope.row.goods_type == 7">砍价</p> -->
                            <p v-else>--</p>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="type_name"
                        label="商品分类"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="shop_title"
                        label="店铺名称"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="company_name"
                        label="公司"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="goods_price"
                        label="售价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="goods_price_origin"
                        label="原价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="goods_stock"
                        label="库存"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="goods_sale"
                        label="真实销量"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="操作"
                        show-overflow-tooltip
                        align="center"
                        v-if="dialogType === 1"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="primary"
                                size="small"
                                @click="handleSelectGoods(scope.row)"
                            >
                                选择
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- /分页 -->
                <div class="pagination-wrapper">
                    <el-pagination
                        background
                        layout="total, prev, pager, next, jumper"
                        :current-page.sync="page.now_page"
                        :total="page.total_count"
                        :page-size="page.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
                v-if="dialogType === 1"
            >
                <el-button
                    @click="dialogVisible = false"
                    size="medium"
                    :style="{width: '126px', marginRight: '52px'}"
                >
                    关 闭
                </el-button>
            </span>
            <span
                slot="footer"
                class="dialog-footer"
                v-if="dialogType === 2"
            >
                <el-button
                    @click="dialogVisible = false"
                    size="medium"
                    :style="{width: '126px', marginRight: '52px'}"
                >
                    取 消
                </el-button>
                <el-button
                    type="primary"
                    :style="{width: '126px'}"
                    @click="confirm()"
                >
                    确 定
                </el-button>
            </span>
        </el-dialog>

        <!-- 图片预览 -->
        <img-preview ref="preview"/>
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';

export default {
    name: 'GoodsSelect',
    components: {
        imgPreview
    },
    data() {
        return {
            loading: true,
            // 1单选 2 多选
            dialogType: 1,
            dialogVisible: false,
            tableData: [],//表格数据
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            // changeData: {},//选择数据
            formData: {
                // 上架状态
                status: true,
                // 商品名称或编号
                keyword: '',
                goodsType: '',
                type_no: ''
            },
            multipleSelection: [],
            // typeList: [{value: 1, label: '自营'},{value: 3, label: '秒杀'},{value: 5, label: '折扣专区'},{value: 7, label: '砍价'}]
            goodsTypeList: [
                {label: '自营', value: 1},
                {label: '淘客', value: 2},
                {label: '折扣', value: 5}
            ],
            classifyList: []
        };
    },
    watch: {},
    mounted() {
        this.getClassifyList();
    },
    methods: {
        /**
         * @description 显示弹窗
         * @param type
         */
        show(type = 1) {
            this.dialogType = type;
            this.dialogVisible = true;
            this.getTableData(1);
        },

        /**
         * @deprecated 编辑商品
         *  list: 选择商品
         */
        edit (type = 1, list) {
            this.dialogType = type;
            this.dialogVisible = true;
            this.getTableData(1);

            if(!list.length) return;
            setTimeout(() => {
                this.multipleSelection = list;
                this.multipleSelection.forEach(el => {
                    this.$refs.couponChangeShop.toggleRowSelection(el, true);
                });
            }, 200);
        },

        /**
         * @description 关闭弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * @description 获取商品数据
         */
        getTableData(page) {

            this.loading = true;
            let type_no = '';

            if(this.formData.type_no.length !== 0) {
                type_no = this.formData.type_no[this.formData.type_no.length - 1];
            }
            this.$post('/student/shop_goods%5Cget_list_for_select', {
                page_id: page || this.page.now_page,
                only_sale: this.formData.status ? 1 : 0,
                keyword: this.formData.keyword,
                goods_type: this.formData.goodsType,
                type_no: type_no
            }, resp => {
                if (resp.code === 1) {
                    if (Object.prototype.toString.call(resp.data) == '[object Null]') return false;
                    this.loading = false;
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 刷新
         */
        refresh() {
            this.loading = true;
            this.checkedAll = false;
            this.getTableData();
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 商品管理
         */
        goShopList() {
            let routeData = this.$router.resolve({
                name: 'GoodsList'
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 批量确定提交
         */
        confirm() {
            if(this.multipleSelection.length === 0) {
                this.$message.warning('请先选择商品');
                return;
            }
            this.$emit('change', this.multipleSelection);
            this.close();
        },

        /**
         * 单个商品选择确定
         * @param goods
         */
        handleSelectGoods(goods) {
            this.$emit('change', goods);
            this.close();
        },

        /**
         * 表格多选事件
         * @param val
         */
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * 获取商品分类列表
         */
        getClassifyList() {
            this.$post('/student/shop_goods_type%5Cget_list_linkage', {}, resp => {
                if (resp.code === 1) {
                    this.classifyList = resp.data.list;
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.couponChangeShop {
    .dialog-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 10px;
    }

    .wrap {
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        height: 500px;

        .headerNewBos {
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-sizing: border-box;
            margin-bottom: 24px;

            .btn-view, .filter-view {
                display: flex;
                align-items: center;
            }

            .header_system {
                color: rgba(24, 144, 255, 1);
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                white-space: nowrap;
            }

            .header_refresh {
                color: rgba(24, 144, 255, 1);
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                margin-left: 32px;
                margin-right: 122px;
                white-space: nowrap;
            }

            .filter {
                width: 372px;
                height: 36px;
                overflow: hidden;
                box-sizing: border-box;
                border-radius: 4px;
                border: 1px #c2c2c2 solid;
                display: flex;
                align-items: center;
                margin-left: 16px;

                .filter_type {
                    flex: 1;
                }

                .filter_line {
                    width: 0px;
                    height: 14px;
                    border-left: 1px #999 solid;
                }
            }
        }

        .tableDate {
            .info {
                display: flex;
                align-items: center;
            }

            .info_main {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                height: 64px;
                align-items: flex-start;

                .info_title {
                    color: rgba(51, 51, 51, 1);
                    font-size: 12px;
                    line-height: 18px;
                    width: 164px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    justify-content: flex-start;
                    text-align: left;
                }

                .info_price {
                    color: rgba(245, 51, 65, 1);
                    font-size: 14px;
                    height: 20px;
                    line-height: 20px;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            background-color: #fbfbfb;
            box-sizing: border-box;
            border-left: 1px #EBEEF5 solid;
            border-right: 1px #EBEEF5 solid;
            border-bottom: 1px #EBEEF5 solid;
            padding: 21px 26px 10px 11px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
}
</style>
<style lang="less">
.couponChangeShop {
    .el-dialog__body {
        padding: 24px 32px;
    }

    .filter_keyword {
        width: 100%;

        .el-input__inner {
            border: none;
        }
    }

    .filter_type {
        .el-input__inner {
            border: none;
        }
    }

    .el-table__body-wrapper {
        height: auto !important;
        max-height: 350px;
    }

    .el-table__row {
        min-height: 85px;
        height: 85px;
    }

    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }

    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }

    .el-table .el-table_1_column_6 .cell {
        position: relative;
    }

    .el-range-editor--small .el-range-separator {
        padding: 0 1px !important;
    }

    .el-table th, .el-table tr {
        background-color: #fbfbfb;
    }

    .placeStyle {
        &::-webkit-input-placeholder {
            font-size: 12px;
        }
    }
}
</style>
